//
//  YFDetailDongtaiHeaderView.m
//  project
//
//  Created by yefei on 2019/5/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFDetailDongtaiHeaderView.h"
#import "YFJourneyModel.h"
#import "YFLocationManager.h"
#import "YBImageBrowser.h"

@interface YFDetailDongtaiHeaderView ()

/**
 背景icon
 */
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;

/**
 头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *avatorImageView;

/**
 名字
 */
@property (weak, nonatomic) IBOutlet UILabel *namLabel;

/**
 性别
 */
@property (weak, nonatomic) IBOutlet UIImageView *sexImageView;

/**
 粉丝数
 */
@property (weak, nonatomic) IBOutlet UILabel *fensLabel;

/**
 关注按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *guanzhu;

@end
@implementation YFDetailDongtaiHeaderView

+ (YFDetailDongtaiHeaderView *)headerView {
    YFDetailDongtaiHeaderView *view = [[UINib nibWithNibName:@"YFDetailDongtaiHeaderView" bundle:nil] instantiateWithOwner:nil options:nil].firstObject;
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, [self defulatHeight]);
    return view;
}

+(CGFloat)defulatHeight {
    return SCREEN_WIDTH * 3 / 4;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    // 头像圆角
    [self.avatorImageView rounded:(UIRectCornerAllCorners) radius:self.avatorImageView.height * 0.5 borderColor:UIColor.blackColor borderWidth:1];
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(photoBrower:)];
    [self.avatorImageView addGestureRecognizer:tapGesture];
    
    [self.guanzhu rounded:UIRectCornerAllCorners radius:3];
    @weakify(self)
    [[self.guanzhu rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.guanzhu.userInteractionEnabled = NO;
        @strongify(self)
        NSString * url;
        if (!self->_userModel.concern) {
            url = [Api stringByAppendingString:ConcernApi];
        }else {
            url = [Api stringByAppendingString:CancelConcernApi];
        }
        [self requestDataWithUrl:url];
    }];
}
#pragma mark-- 关注和取消关注
-(void)requestDataWithUrl:(NSString *)url {
    @weakify(self)
    [HJHttpTool post:url params:@{@"userId":self->_userModel.userId} success:^(HJResponeds *result) {
        @strongify(self)
        self.guanzhu.selected = !self.guanzhu.selected;
        self->_userModel.concern = self.guanzhu.selected;
        [mNotificationCenter postNotificationName:NotificationAttention object:nil userInfo:@{@"userId":self->_userModel.userId,@"concern":@(self->_userModel.concern)}];
        [self loadDataWithDic:@{@"userId": self->_userModel.userId}];
    } failure:^(id result) {
        [YFHud showError:result];
    }];
}

-(void)photoBrower:(UIGestureRecognizer *)gesture {
    NSMutableArray <YBImageBrowseCellData *>* imageSource = [NSMutableArray array];
    YBImageBrowseCellData *ImageData = [YBImageBrowseCellData new];
    ImageData.url = _userModel.avatarURL;
    ImageData.sourceObject = self.avatorImageView;
    [imageSource addObject:ImageData];
    
    YBImageBrowser * imageBrowser = [YBImageBrowser new];
    [imageBrowser.defaultToolBar hideOperationButton];
    [imageBrowser.defaultSheetView setHidden:YES];
    imageBrowser.dataSourceArray = imageSource;
    imageBrowser.currentIndex = 0;
    [imageBrowser show];
}

-(void)setUserModel:(YFUserModel *)userModel {
    _userModel = userModel;
    [self loadDataWithDic:@{@"userId":userModel.userId}];
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:userModel.backgroundIcon]];
    [self.avatorImageView sd_setImageWithURL:userModel.avatarURL];
    if (userModel.avatar.length == 0) {
        self.avatorImageView.image = [UIImage imageNamed:@"img_mydongtai_defualt_avatar"];
    }
    
    self.namLabel.text = userModel.nickName;
    self.sexImageView.image = userModel.sex.intValue == 1 ? [UIImage imageNamed:@"ico_mine_male"] : [UIImage imageNamed:@"ico_mine_female"];;
    self.guanzhu.selected = userModel.concern;
}

#pragma mark - 网络请求
- (void)loadDataWithDic:(NSDictionary *)dic {
    NSString * url = [Api stringByAppendingString:personalCenterApi];
    @weakify(self)
    [HJHttpTool get:url params:dic success:^(HJResponeds *result) {
        @strongify(self)
        dispatch_async(dispatch_get_main_queue(), ^{
            self.guanzhu.userInteractionEnabled = YES;
            NSDictionary * dic = result.data;
            if ([dic.allKeys containsObject:@"myConcernAmount"]) {
                self.fensLabel.text = [NSString stringWithFormat:@"粉丝：%@",dic[@"othersConcernAmount"]];
            }
        });
    } failure:^(id result) {
    }];
}
@end
